﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using VSSAHT.DataAccess;
using VSSAHT.Transfer;
namespace VSS_AHT
{
    public partial class FormRequireNumber : BaseForm
    {
        #region Ctors
        private static volatile FormRequireNumber instance;
        private static object syncRoot = new Object();

        public static FormRequireNumber Instance
        {
            get
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new FormRequireNumber();
                }

                return instance;
            }
        }

        public FormRequireNumber()
        {
            InitializeComponent();
            Initialize();
        }
        public void Initialize()
        {
            this.rdbRequireCode.CheckedChanged += new EventHandler(delegate { ClickRdbLocationCode(); });
            this.rdbRequireName.CheckedChanged += new EventHandler(delegate { ClickRdbLocationCode(); });
            this.btnSearch.Click += new EventHandler(delegate { ClickButtonSearch(); });
            this.btnReset.Click += new EventHandler(delegate { ClickButtonReset(); });

            this.btnNew.Click += new EventHandler(delegate { ClickButtonNew(); });
            this.btnEdit.Click += new EventHandler(delegate { ClickButtonEdit(); });
            this.btnRemove.Click += new EventHandler(delegate { ClickButtonDelete(); });
            this.btnClose.Click += new EventHandler(delegate { this.Close(); });
            this.Disposed += new EventHandler(delegate { instance = null; });
            this.ClickButtonSearch();
        }
        #endregion

        #region Field Members
        RequireNumberDataAccess _dataAccess = new RequireNumberDataAccess();

        IList<RequireNumberDTO> _RequireList;
        #endregion

        #region Methods
        private void ClickButtonSearch()
        {

            _dataAccess.SearchObject = new RequireNumberDTO();
            _dataAccess.SearchObject.RequireCode = this.txtRequireCode.Text;
            _dataAccess.SearchObject.RequireName = this.txtRequireName.Text;

            _RequireList = _dataAccess.Search();

            this.gridControl1.DataSource = _RequireList; ;

        }

        private void ClickRdbLocationCode()
        {
            if (rdbRequireCode.Checked)
            {
                this.txtRequireCode.Enabled = true;
                this.txtRequireName.Enabled = false;
                this.txtRequireCode.Focus();
            }
            else
            {
                this.txtRequireCode.Enabled = false;
                this.txtRequireName.Enabled = true;
                this.txtRequireName.Focus();
            }
        }

        private void ClickButtonReset()
        {
            this.txtRequireCode.Text = string.Empty;
            this.txtRequireName.Text = string.Empty;
            this.ClickButtonSearch();
        }

        private void ClickButtonNew()
        {
            int count = _RequireList.Count;
            // Show the updating staff instance
            FormUpdateRequireNumber instance = new FormUpdateRequireNumber();
            instance.Collection = _RequireList;
            instance.ShowDialog();

            if (count < _RequireList.Count)
            {
                // Refresh data
                this.ClickButtonSearch();
            }
        }

        private void ClickButtonEdit()
        {
            if (this.gridView1.GetFocusedRow() == null)
            {
                Common.ShowAlert("Vui lòng cho dòng cần chỉnh sửa");
                return;
            }

            // Edit  information
            FormUpdateRequireNumber instance = new FormUpdateRequireNumber();
            instance.Require = this.gridView1.GetFocusedRow() as RequireNumberDTO;
            DialogResult result = instance.ShowDialog();
            if (result == DialogResult.OK)
            {
                // Refresh data
                this.gridView1.RefreshData();
            }
        }

        private void ClickButtonDelete()
        {
            try
            {
                if (this.gridView1.GetFocusedRow() == null)
                {
                    Common.ShowAlert("Vui lòng chọn dòng cần xóa!");
                    return;
                }

                RequireNumberDTO require = this.gridView1.GetFocusedRow() as RequireNumberDTO;
                bool? result = Common.ShowYesNoQuestion("Bạn có chắc chắn muốn xóa thông tin này?");
                if (result.HasValue && result.Value)
                {
                    string requireCode = require.RequireCode;
                    bool r = _dataAccess.Delete(require);

                    if (r)
                    {
                        this.gridView1.DeleteSelectedRows();
                        this.gridView1.RefreshData();
                    }
                    else
                    {
                        Common.ShowAlert("Quá trình xóa số yêu cầu không thành công.");
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion

       
        
    }
}